const Koa = require('./my-koa/lib/application')
const fs = require('fs')
const path = require('path')

let app = new Koa()

const sleep = function (time) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            console.log('sleep')
            resolve()
        }, time)
    })
}



app.use(async (ctx, next) => {
    console.log(1,)
    await next()
    console.log(2)
})

app.use(async (ctx, next) => {
    console.log(3,)
    await sleep(2000)
    console.log(ctx.path)
    ctx.body = fs.createReadStream(path.resolve(__dirname, 'node.md'))
    await next()
    console.log(4)
})

app.use(async (ctx, next) => {
    console.log(5,)
    next()
    console.log(6)
})
app.on('error', function (e) {
    console.log('error ' + e)
})

app.listen(3000, function () {
    console.log('server start 300')
})
/** 
 * 几种情况
 *  不加sleep  135642
 *  +sleep   135642
 *  + await + sleep 132564
 *  await 包含await sleep的next  13 sleep 5642 
 *  return 包含await sleep的next  13 sleep 564  因为return Promise
 *  */